﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Rw.Core.Common.Paging;
using Rw.Core.Common;
using WH.Common.Paging;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Mapster;
using WH.WebApi.Controllers;
using WH.Contracts.Systems.OperationLog;
using WH.Services.IServices.ISystem;
using SqlSugar;
using WH.Model.Systems;

namespace WH.WebApi.Controllers.Systems
{
    /// <summary>
    /// 操作日志
    /// </summary>
    [Authorize, Route("OperationLog"), ApiController]
    public class OperationLogController : BaseController
    {
        private readonly ISysOperationLogService _sysOperationLogService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysOperationLogService"></param>
        public OperationLogController(ISysOperationLogService sysOperationLogService)
        {
            _sysOperationLogService = sysOperationLogService;
        }

        /// <summary>
        /// 获取记录分页列表
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpGet("Page")]
        public async Task<ResultResponse<IPagedList<OperationLogOutputDto>>> GetPage([FromQuery] PageParam pager, [FromQuery] OperationLogFilterDto filter)
        {

            var exp = Expressionable.Create<SysOperationLogEntity>();

            if (!string.IsNullOrWhiteSpace(filter.Keywords))
            {
                exp.And(x => x.IpAddress.Contains(filter.Keywords) || x.RequestUrl.Contains(filter.Keywords) || x.UserAccount.Contains(filter.Keywords));
            }

            var pagedList = await _sysOperationLogService.PagedFindAllAsync(exp.ToExpression(), pager);

            var pages = pagedList.Entities;

            var reslist = pages.Select(item =>
            {
                return item.Adapt<OperationLogOutputDto>();
            }).ToList();

            var data = new PagedList<OperationLogOutputDto>()
            {
                PageIndex = pagedList.PageIndex,
                PageSize = pagedList.PageSize,
                TotalCount = pagedList.TotalCount,
                Entities = reslist
            };

            return Success<IPagedList<OperationLogOutputDto>>(data);
        }


        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("")]
        public async Task<ResultResponse> Delete([FromBody] List<string> ids)
        {
            var exp = Expressionable.Create<SysOperationLogEntity>();

            if (ids.Contains("*"))
            {
                exp.And(x => true);
            }
            else
            {
                if (ids is not null && ids.Any())
                {
                    exp.And(x => ids.Contains(x.Id));
                }
            }

            var res = await _sysOperationLogService.RemoveAsync(exp.ToExpression());

            return res > 0 ? Success() : Error();
        }
    }
}
